package cate.game.play.skill.passive.awaken;

import cate.common.table.d.GDHero;
import cate.common.util.XT;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 效果1：觉醒状态下每次格挡成功根据职业获得属性提升：战士和法师提升2.7%伤害加成,坦克和辅助提升2.7%伤害减免,不可驱散,最多10层,持续到战斗结束;
 *       格挡失败则恢复自身3%最大生命值，每回合最多3次
 * 效果2：觉醒状态下成功格挡增加自身25%觉醒值,每回合最多触发1次;
 *       受击时未触发格挡则有50%概率随机降低1名敌方单位25%觉醒值,每回合最多触发1次
 * 效果3：觉醒状态下记录格挡的伤害,格挡3次后将记录伤害均摊给随机3个敌方目标,每个觉醒状态只生效1次
 */
public class 荆棘屏障PH extends PassiveHandler {

	private int 战法buff;
	private int 坦辅buff;
	private double 恢复系数;
	private int 回合次数;

	private double 觉醒提升;
	private int 降低概率;
	private double 觉醒降低;

	private int 格挡次数;
	private int 格挡伤害目标;

	//战法buff=？？&坦辅buff=？？&恢复系数=300&回合次数=3
	//&觉醒提升=25&降低概率=5000&觉醒降低=25&
	//&格挡次数=3&格挡伤害目标=3
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		战法buff = args.getInt("战法buff", 0);
		坦辅buff = args.getInt("坦辅buff", 0);
		恢复系数 = args.getWDouble("恢复系数", 0d);
		回合次数 = args.getInt("回合次数", 0);

		觉醒提升 = args.getDouble("觉醒提升", 0d);
		降低概率 = args.getInt("降低概率", 0);
		觉醒降低 = args.getDouble("觉醒降低", 0d);

		格挡次数 = args.getInt("格挡次数", 0);
		格挡伤害目标 = args.getInt("格挡伤害目标", 0);
	}

	@Override
	public void afterSufferOneAttack(SkillActionCtx action, long totalDamage, boolean strike) {
		if (skill.owner.isDead() || !skill.owner.status.isAwaken()) {
			return;
		}
		if (action.getStat().isParry(skill.owner.getPid())) {
			onParry(action);
		} else {
			onParryFail(action);
		}
	}

	@Override
	public void onAwaken(ActionCtx action, boolean start) {
		if (start) {
			parryNum = 0;
			parryDamage = 0d;
		}
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		healNum = 0;
		addEnergy = false;
		reduceEnergy = false;
	}

	private int healNum;
	private boolean addEnergy;
	private boolean reduceEnergy;

	private int parryNum;
	private double parryDamage;


	private void onParry(SkillActionCtx action) {
		int buff = skill.owner.getJob() <= GDHero.Job.J2 ? 战法buff : 坦辅buff;
		if (buff > 0) {
			skill.owner.buff.tryAddByTid(action, buff);
		}
		if (!addEnergy && 觉醒提升 > 0) {
			addEnergy = true;
			skill.owner.skill.addAwakenEnergy(action, 觉醒提升);
		}

		if (格挡次数 > 0) {
			if (parryNum < 格挡次数) {
				parryNum++;
				parryDamage += action.getStat().getTarget(skill.owner.getPid()).parryDamage();
				if (parryNum == 格挡次数) {
					double damage = parryDamage / 格挡伤害目标;
					for (Fighter target : TargetSelector.doFind(skill.owner, action.getTeamVs(), new TargetFilterParam().targetNumSet(格挡伤害目标))) {
						target.attr.passiveHurt(action, skill.owner, damage);
					}
				}
			}
		}
	}


	private void onParryFail(SkillActionCtx action) {
		if (healNum < 回合次数) {
			healNum++;
			double healValue = skill.owner.attr.total().hpTotal() * 恢复系数;
			skill.owner.attr.passiveHeal(action, healValue);
		}
		if (!reduceEnergy && 降低概率 > 0) {
			if (XT.chance(降低概率)) {
				Fighter one = TargetSelector.findOne(skill.owner, action.getTeamVs(), new TargetFilterParam());
				if (one != null) {
					one.skill.addAwakenEnergy(action, -觉醒降低);
					reduceEnergy = true;
				}
			}
		}
	}
}
